﻿Imports System.Windows.Forms
Imports System.Drawing

Public Class vMsg
    'Const minWidth As Integer = 500
    Const maxWidth As Integer = 500
    Private frmicon As System.Drawing.Icon
    ''' <summary>
    ''' Các biến icon của message
    ''' </summary>
    ''' <remarks></remarks>
    Public Enum vIcon As Integer
        vError
        vInformation
        vQuestion
        vWarning
        vCritical
    End Enum

    ''' <summary>
    ''' Các biến button của message
    ''' </summary>
    ''' <remarks></remarks>
    Public Enum vButtons
        vAbortRetryIgnore
        vOK
        vOKCancel
        vRetryCancel
        vYesNo
        vYesNoCancel
    End Enum
    ''' <summary>
    '''   Thêm các result trả lại của msgbox
    ''' </summary>
    ''' <remarks></remarks>
    Public Enum vReSult
        Ok = 1
        Cancel = 2
        Abort = 3
        Retry = 4
        Ignore = 5
        Yes = 6
        No = 7
    End Enum
    ''' <summary>
    ''' Set trạng thái ban đầu của button hiển thị
    ''' </summary>
    ''' <remarks></remarks>
    Public Enum vDefaultButtons
        vButton1
        vButton2
        vButton3
    End Enum

    ''' <summary>
    ''' Set trạng thái hiển thị của button
    ''' </summary>
    ''' <param name="button"></param>
    ''' <remarks></remarks>
    Private Sub SetButtons(ByVal button As vButtons, ByVal vDefaultButton As vDefaultButtons, Optional ByVal CheckVuotKeHoach As Boolean = False)
        Select Case button
            Case vButtons.vOK
                Button3.Visible = True
                Button3.Text = "Đó&ng"
                Button3.DialogResult = DialogResult.OK
                ControlBox = True
            Case vButtons.vAbortRetryIgnore
                Button3.Visible = True
                Button3.Text = "T&iếp tục"
                Button3.DialogResult = DialogResult.Ignore
                Button2.Visible = True
                Button2.Text = "&Thử lại"
                Button2.DialogResult = DialogResult.Retry
                Button1.Visible = True
                Button1.Text = "&Hủy bỏ"
                Button1.DialogResult = DialogResult.Abort
                Select Case vDefaultButton
                    Case vDefaultButtons.vButton1
                        Button1.Select() 'TabIndex = 0
                    Case vDefaultButtons.vButton2
                        Button2.Select() 'TabIndex = 0
                    Case vDefaultButtons.vButton3
                        Button3.Select() 'TabIndex = 0
                End Select
            Case vButtons.vOKCancel
                Button2.Visible = True
                Button2.DialogResult = DialogResult.OK
                Button2.Text = "Đồ&ng ý"
                Button3.Visible = True
                Button3.DialogResult = DialogResult.Cancel
                Button3.Text = "&Bỏ qua"
                Select Case vDefaultButton
                    Case vDefaultButtons.vButton1
                        Button2.Select() 'TabIndex = 0
                    Case vDefaultButtons.vButton2
                        Button3.Select() 'TabIndex = 0
                End Select
            Case vButtons.vRetryCancel
                Button2.Visible = True
                Button2.Text = "T&hử lại"
                Button2.DialogResult = DialogResult.Retry
                Button3.Visible = True
                Button3.Text = "&Thôi"
                Button3.DialogResult = DialogResult.Cancel
                Select Case vDefaultButton
                    Case vDefaultButtons.vButton1
                        Button2.Select() 'TabIndex = 0
                    Case vDefaultButtons.vButton2
                        Button3.Select() 'TabIndex = 0
                End Select
            Case vButtons.vYesNo
                Button2.Visible = True
                Button2.Text = "&Có"
                Button2.DialogResult = DialogResult.Yes
                Button3.Visible = True
                Button3.Text = "&Không"
                Button3.DialogResult = DialogResult.No
                Select Case vDefaultButton
                    Case vDefaultButtons.vButton1
                        If CheckVuotKeHoach Then
                            Button1.Select() 'Neu la thong bao tren form thi focus vao button1, no nhan key enter qua form thi nhay sang button2 la vua
                        Else
                            Button2.Select()    'Khong dung enter tren form nen focus vao button2 luon
                        End If
                        'Button2.Select() 'TabIndex = 0
                    Case vDefaultButtons.vButton2
                        Button3.Select() 'TabIndex = 0
                End Select
            Case vButtons.vYesNoCancel
                Button1.Visible = True
                Button1.DialogResult = DialogResult.Yes
                Button1.Text = "&Có"
                Button2.Visible = True
                Button2.DialogResult = DialogResult.No
                Button2.Text = "&Không"
                Button3.Visible = True
                Button3.DialogResult = DialogResult.Cancel
                Button3.Text = "&Bỏ qua"
                Select Case vDefaultButton
                    Case vDefaultButtons.vButton1
                        Button1.Select() 'TabIndex = 0
                    Case vDefaultButtons.vButton2
                        Button2.TabIndex = 0
                    Case vDefaultButtons.vButton3
                        Button3.Select() 'TabIndex = 0
                End Select
        End Select

    End Sub

    ''' <summary>
    ''' Trạng thái đầu của nút button
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetDefaultButton()

        If Button1.Visible = True And Button2.Visible = True And Button3.Visible = True Then

        End If

    End Sub

    ''' <summary>
    ''' Set trạng thái hiển thị của icon
    ''' </summary>
    ''' <param name="icon"></param>
    ''' <remarks></remarks>
    Private Sub SetIcons(ByVal icon As vIcon)
        If Me.Height < 160 Then
            Me.Height = 160
        End If
        'If Me.Width <= 600 Then
        '    Me.Width += 50
        'End If
        Select Case icon
            Case vIcon.vError
                frmicon = System.Drawing.SystemIcons.Error
                PictureBox1.Image = New Bitmap(frmicon.ToBitmap())
            Case vIcon.vInformation
                frmicon = System.Drawing.SystemIcons.Information
                PictureBox1.BackgroundImage = New Bitmap(frmicon.ToBitmap())
            Case vIcon.vQuestion
                frmicon = System.Drawing.SystemIcons.Question
                PictureBox1.BackgroundImage = New Bitmap(frmicon.ToBitmap())
            Case vIcon.vWarning
                frmicon = System.Drawing.SystemIcons.Warning
                PictureBox1.BackgroundImage = New Bitmap(frmicon.ToBitmap())
        End Select

    End Sub

    ''' <summary>
    ''' Cấu hình message
    ''' </summary>
    ''' <param name="text"></param>
    ''' <param name="caption"></param>
    ''' <param name="vsize"></param>
    ''' <param name="font"></param>
    ''' <param name="isIcon"></param>
    ''' <remarks></remarks>
    Private Sub vMessageShow(ByVal text As String, ByVal caption As String, ByVal vsize As Size, ByVal font As Font, ByVal isIcon As Boolean)
        Dim width As Integer = vsize.Width
        Dim height As Integer = vsize.Height
        Me.Text = caption
        lblMessage.Text = text

        Dim g As Graphics = Me.CreateGraphics
        Dim textSize As SizeF = g.MeasureString(text, font)
        g.Dispose()
        If isIcon = True Then
            pnPicture.Visible = True
            PictureBox1.Visible = True
            If pnPicture.Width + textSize.Width + 20 <= vsize.Width Then
                width = vsize.Width
            End If

            If vsize.Width < pnPicture.Width + textSize.Width + 33 And pnPicture.Width + textSize.Width + 33 < maxWidth Then
                width = pnPicture.Width + textSize.Width + 33
            End If

            If pnPicture.Width + textSize.Width + 33 > maxWidth Then
                width = maxWidth
            End If

        Else
            pnPicture.Visible = False
            PictureBox1.Visible = False
            If textSize.Width + 20 <= vsize.Width Then
                width = vsize.Width
            End If

            If vsize.Width < textSize.Width + 33 And textSize.Width + 33 < maxWidth Then
                width = textSize.Width + 33
            End If

            If textSize.Width + 33 > maxWidth Then
                width = maxWidth
            End If

        End If

        Me.Size = New Size(width, height)
    End Sub

    ''' <summary>
    ''' Hiển thị nội dung thông báo
    ''' </summary>
    ''' <param name="Text">Nội dung</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overloads Shared Function Show(ByVal Text As String) As DialogResult
        Dim v As New vMsg
        v.vMessageShow(Text, "Thông báo", v.Size, v.Font, True)
        v.SetButtons(vButtons.vOK, vDefaultButtons.vButton3)
        v.SetIcons(vIcon.vInformation)
        Return v.ShowDialog()
    End Function

    ''' <summary>
    ''' Hiển thị nội dung thông báo và tiêu đề
    ''' </summary>
    ''' <param name="Text">Nội dung</param>
    ''' <param name="Caption">Tiêu đề</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overloads Shared Function Show(ByVal Text As String, ByVal Caption As String) As DialogResult
        Dim v As New vMsg
        v.vMessageShow(Text, Caption, v.Size, v.Font, True)
        v.SetButtons(vButtons.vOK, vDefaultButtons.vButton1)
        v.SetIcons(vIcon.vInformation)
        Return v.ShowDialog()
    End Function

    ''' <summary>
    ''' Hiển thị nội dung thông báo, tiêu đề và nút button
    ''' </summary>
    ''' <param name="Text">Nội dung</param>
    ''' <param name="Caption">Tiêu đề</param>
    ''' <param name="button">Nút button</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overloads Shared Function Show(ByVal Text As String, ByVal Caption As String, ByVal button As vButtons) As DialogResult
        Dim v As New vMsg
        v.vMessageShow(Text, Caption, v.Size, v.Font, False)
        v.SetButtons(button, vDefaultButtons.vButton1)
        Return v.ShowDialog()
    End Function

    ''' <summary>
    ''' Hiển thị nội dung thông báo, tiêu đề, nút button và Icon
    ''' </summary>
    ''' <param name="Text">Nội dung</param>
    ''' <param name="Caption">Tiêu đề</param>
    ''' <param name="button">Nút button</param>
    ''' <param name="icon">Biểu tượng</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overloads Shared Function Show(ByVal Text As String, ByVal Caption As String, ByVal button As vButtons, ByVal icon As vIcon) As DialogResult
        Dim v As New vMsg
        v.vMessageShow(Text, Caption, v.Size, v.Font, True)
        v.SetButtons(button, vDefaultButtons.vButton1)
        v.SetIcons(icon)
        v.StartPosition = FormStartPosition.CenterScreen
            Return v.ShowDialog()
    End Function

    ''' <summary>
    ''' Hiển thị nội dung thông báo, tiêu đề, nút button, Icon và button mặc định được focus
    ''' </summary>
    ''' <param name="Text">Nội dung</param>
    ''' <param name="Caption">Tiêu đề</param>
    ''' <param name="button">Nút button</param>
    ''' <param name="icon">Biểu tượng</param>
    ''' <param name="vDefaultButton"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overloads Shared Function Show(ByVal Text As String, ByVal Caption As String, ByVal button As vButtons, ByVal icon As vIcon, ByVal vDefaultButton As vDefaultButtons, Optional ByVal CheckVuotKeHoach As Boolean = False) As DialogResult
        Dim v As New vMsg
        v.vMessageShow(Text, Caption, v.Size, v.Font, True)
        v.SetButtons(button, vDefaultButton, CheckVuotKeHoach)
        v.SetIcons(icon)
        Return v.ShowDialog()
    End Function

    Private Sub vMsg_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
    End Sub
 
    Private Sub vMsg_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class
